package sfsoftware.game.actor.item;

import java.util.Vector;

import sfsoftware.game.TrevorLegacy;
import sfsoftware.game.actor.attributes.Collidable;
import sfsoftware.game.actor.attributes.Obtainable;
import sfsoftware.game.trevor.Player;
import sfsoftware.util.Actor;
import sfsoftware.util.Sound;
import sfsoftware.util.Updatable;
import sfsoftware.util.graphics.Graphics;

public class Ruple extends Actor implements Obtainable, Updatable
{
	private boolean pickedUp = false;
	private double speed = 1;
	private float alpha = 1;
	private int value = 1;
	private static Vector<Sparkle> sparkles = new Vector<Sparkle>();
	
	public Ruple(int destX, int destY, String texture, int destWidth, int destHeight) 
	{
		super(destX, destY, texture, destWidth, destHeight);
	}

	public Ruple(int destX, int destY, String texture, int srcX, int srcY, int destWidth, int destHeight) {
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight);
		switch(srcX)
		{
			case 16:
				value = 5;
				break;
			case 32:
				value = 10;
				break;
			case 48:
				value = 20;
				break;
		}
	}

	@Override
	public void collisionResult(Collidable object)
	{
		if(!(pickedUp)) 
		{
			// Player should get money here!
			Sound.playSound("ruple");
			Player.addToRuples(value);
			
			// Set the ruple as picked up
			pickedUp = true;
		}
	}

	@Override
	public void draw()
	{
		if(pickedUp)
		{
			// If the ruple is totally faded out - remove it from the cast
			if(alpha <= 0)
			{
				Actor.cast.remove(this);
			}
			else
			{
				super.draw(getWidth(), getHeight(), getSrcX(), getSrcY(), width, height, 1, 1, 1, alpha);
			}
		}
		else
		{					
			super.draw();
			
			Sparkle[] s = new Sparkle[sparkles.size()];
			sparkles.toArray(s);			
			for(int i = 0; i < s.length; i++)		
			{
				s[i].draw();
			}			
		}
	}
	
	private double getAngleToHUDRuple()
	{
		double dx = (getX() + (getWidth() / 2)) - (345 + (getWidth() / 2));
		double dy = (getY() + (getHeight() / 2)) - (TrevorLegacy.HUDy + (getHeight() / 2));
		
		double inRads = Math.atan2(dy,dx);
		
		if (inRads < 0)
		{
	        inRads = Math.abs(inRads);
		}
	    else
	    {
	        inRads = 2 * Math.PI - inRads;
	    }			
		
		return inRads;
	}		
	
	/**
	 * If the Ruple was picked up, move it towards the HUD and fade it out!
	 */
	@Override
	public void update() {
		if(pickedUp)
		{
			double directionAngle = getAngleToHUDRuple();
			double xDirection = Math.cos(directionAngle) * -1;
			double yDirection = Math.sin(directionAngle);	
			
			alpha = alpha - 0.025f;
			speed = speed + 0.15d;
			
			super.moveWithoutCheckingCollision(xDirection * speed, yDirection * speed);
		}	
		else if(((int) (Math.random() * 100)) <= 0)
		{
			new Sparkle();		
		}
	}
	
	private class Sparkle
	{
		private int x;
		private int y;
		private sfsoftware.util.Clock c;
		private int sparkleState = 0;
		public Sparkle() 
		{
			x = (int) (getX() + (int) (Math.random() * getWidth()));
			y = (int) (getY() + (int) (Math.random() * getHeight()));
			c = new sfsoftware.util.Clock(50);
			sparkles.add(this);
		}		
		public void draw()
		{
			switch(sparkleState)
			{
				case 0:
					Graphics.drawQuad(x, y, 1, 1, 1, 1, 1, 1);
					break;
				case 1:
					Graphics.drawQuad(x - 1, y, 3, 1, 1, 1, 1, 1);
					Graphics.drawQuad(x, y - 1, 1, 3, 1, 1, 1, 1);					
					break;
				case 2:
					Graphics.drawQuad(x - 2, y, 5, 1, 1, 1, 1, 1);
					Graphics.drawQuad(x, y - 2, 1, 5, 1, 1, 1, 1);	
					break;
				case 3:
					Graphics.drawQuad(x - 1, y, 3, 1, 1, 1, 1, 1);
					Graphics.drawQuad(x, y - 1, 1, 3, 1, 1, 1, 1);					
					break;
				case 4:
					Graphics.drawQuad(x, y, 1, 1, 1, 1, 1, 1);
					break;					
				default:
					sparkles.remove(this);
			}
			if(c.updateClock())
			{
				sparkleState++;
			}
		}		
	}
	
}
